*****************************************************************
*****************************************************************
*** 0. General settings
*****************************************************************
*****************************************************************

* clear
clear
clear 		mata
mat 		drop _all

* set path
global		path	""					




use "$path/0 Data/_Processed/BHC_Bank_Ratios.dta", clear


merge m:m entity using "$path/0 Data/_Processed/sample"
keep if _merge == 3
drop _merge

tsset entity yq
sort entity yq
by entity: gen change_unused_ci = (offbs_unused_loans - l.offbs_unused_loans) * (-1)
by entity: gen change_deposits = (liab_deposits_total - l.liab_deposits_total)
by entity: gen net_drawdowns = (change_unused_ci - change_deposits) / l.assets_total

by entity: gen change_unused_ci_pc = ((offbs_unused_loans - l.offbs_unused_loans)/l.offbs_unused_loans)* (-1)


order change_unused_ci change_deposits net_drawdowns

drop if net_drawdowns  == .
egen group = group(entity)
sum group

collapse (mean) y1 = net_drawdowns y2 = change_unused_ci_pc (semean) se_y1 = net_drawdowns se_y2 = change_unused_ci_pc  [aweight= assets_total],by(yq)


gen y1_u =  y1 + 1.96*se_y1
gen y1_d =  y1 - 1.96*se_y1

gen y2_u =  y2 + 1.96*se_y2
gen y2_d =  y2 - 1.96*se_y2


tsset yq

		twoway (tsline y1) (rcap y1_u  y1_d yq) , ///
		scheme(s2mono) ///
		xtitle("Date") 	xline(21997)	///
		ytitle("Net Drawdowns (% Total Assets)") ///
		legend(off) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 



graph export "$path/05 Figures/Figure 4 Panel A.png", as(png) name("Graph") replace
 
* remove 2 outliers in SE (Q1 2014 - check!!)
replace y2_u = . in 5
replace y2_d = . in 5
replace y2_u = . in 22
replace y2_d = . in 22


		twoway (tsline y2) (rcap y2_u  y2_d yq) , ///
		scheme(s2mono) ///
		xtitle("Date") 	xline(21997)	///
		ytitle("Gross Drawdowns (% Total Assets)") ///
		legend(off) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 



graph export "$path/05 Figures/Figure 4 Panel B.png", as(png) name("Graph") replace
 

 
 